<template>
  <div class="content">
    <van-nav-bar
      title="标题"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />

    <div class="box" v-for="(item, index) in addressList" :key="index">
      <van-swipe-cell>
        <div @click.stop="onSelect(item.id)" class="add_list">
          <van-icon name="edit" class="edit" @click.stop="onEdit(item.id)" />
          {{ item.name }},{{ item.tel }}

          <div class="isdefault" v-if="item.isDefault">默认</div>
        </div>
        <template #right>
          <van-button square text="删除" type="danger" class="delete-button"  @click="delAddress(item.id)"/>
        </template>
      </van-swipe-cell>
    </div>

    <!-- </van-swipe-cell>
    <van-contact-list
      v-model="addressList"
      :list="addressList"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="onSelect"
    /> -->
    <van-button
    color="linear-gradient(to right, #ff6034, #ee0a24)" 
      square
      text="新增地址"
      type="danger"
      class="add_address"
      @click="onAdd"
    />
  </div>
</template>

<script>
import { getAddress ,delOneAddress} from "@api";
export default {
  data() {
    return {
      addressList: [],
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    onAdd() {
      //

      this.$router.push("/Add_address");
    },
    onEdit(contact) {
      console.log(contact, 111);
      this.$router.push("/receivingaddress?id=" + contact);
      //
    },
    onSelect(contact) {
      console.log(contact);
      this.$router.replace("/placeorder?id=" + contact);
    },
   

    
    delAddress(id){
      console.log(id);
      delOneAddress(id).then(res=>{
        this.$router.go(0)
      })
    }
  },

  created() {
    getAddress().then((res) => {
      if (res.data.code === 200) {
        let arr = res.data.data;
        arr.forEach((item) => {
          if (JSON.parse(item.content).name) {
            item.contact = JSON.parse(item.content);
            item.contact.id = item.id;
            this.addressList.push(item.contact);
            console.log(item.content);
          }
        });
        console.log("成功获取所有收获地址");
      }
    });
  },
};
</script>

<style lang="scss" scoped>
.van-nav-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
}
.box{
 
  overflow: hidden;
   margin-bottom: 15px;
   
     margin-top: 20px;
     margin: 20px 5%;
      border-radius:10px ;
}
.content {
  overflow-y: auto;
  padding-top: 60px;
 
  box-sizing: border-box;
  .add_list {
   
    width: 100%;
    text-align: left;

   
    background: #fff;

    padding: 10px 0 10px 20px;
    
    width: 100%;
    
    .goods-card {
      margin: 0;
      background-color: white;
    }

    // .delete-button {
    //   height: 100%;
    // }
  }

  .add_address {
    width: 80%;
    border-radius: 20px;
    margin-top: 50px;
  }
}

.isdefault {
  background-color: red;
  display: inline-block;
  border-radius: 10px;

  width: 30px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  position: absolute;
  right: 30%;

  color: #fff;
  font-size: 12px;
}
.van-icon {
  float: right;
  margin-right: 20px;
  display: block;
  height: 25px;
  width: 60px;
  text-align: center;
  top: -5px;
  line-height: 30px;
}
</style>